/**
 * 
 */
package de.pd.pjim2.controller.gui.dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;
import org.jdesktop.application.ResourceMap;

import de.pd.pjim2.controller.PJWindowController;

/**
 * @author petros
 * 
 */
@SuppressWarnings("serial")
public class UserGuideDialog extends JDialog {

	private final JPanel mcontentPanel = new JPanel();

	/**
	 * Create the dialog.
	 */
	public UserGuideDialog(JFrame pFrame) {
		super(pFrame, true);
		setTitle("User Guide");
		ResourceMap resourceMap = Application
				.getInstance(PJWindowController.class).getContext()
				.getResourceMap(PJWindowController.class);
		ApplicationActionMap actionMap = Application.getInstance().getContext()
				.getActionMap(this);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		mcontentPanel.setLayout(new GridLayout());

		try {
			InputStream inputStream = UserGuideDialog.class
					.getResourceAsStream("/de/pd/pjim2/controller/resources/guide.html");
			InputStreamReader is = new InputStreamReader(inputStream);
			BufferedReader br = new BufferedReader(is);
			String read = br.readLine();
			StringBuffer sb = new StringBuffer();
			while (read != null) {
				sb.append(read);
				read = br.readLine();
			}
			String uri = UserGuideDialog.class.getResource("/de/pd/pjim2/controller/resources/workspace.jpg").toURI().toString();
			JLabel lblHtml = new JLabel(sb.toString().replaceAll("REC_PATH", uri));
			JScrollPane sp = new JScrollPane(lblHtml);
			mcontentPanel.add(sp);
		} catch (Exception e) {
			e.printStackTrace();
		}

		getContentPane().add(mcontentPanel, BorderLayout.CENTER);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton(actionMap.get("okAction"));
				okButton.setText("Close");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

	@Action
	public void okAction() {
		dispose();
	}

}
